﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
 
using Kaiser.Models;

namespace Xoohoo.Models.InputModels
{
    public class UserGroupInput
    {
        [DisplayName("用户组ID")]
        public Guid? UserGroupID { get; set; }

        [DisplayName("所属用户组")]
        public Guid? ParentID { get; set; }

        [Required(ErrorMessage = "用户组名称不能为空")]
        [StringLength(50, ErrorMessage = "用户组名称请保持在50个字符以内")]
        //[SlugWithChinese(ErrorMessage = "用户组名称只能包含中文、字母、数字、_和-")]
        [DisplayName("用户组名称")]
        public string UserGroupName { get; set; }
        public IEnumerable<Guid> Roles { get; set; }

        public static UserGroupInput FromUserGroup(UserGroup userGroup)
        {
            if (userGroup == null) return null;
            return new UserGroupInput
            {
                ParentID = userGroup.ParentID,
                UserGroupName = userGroup.UserGroupName,
                Roles = from r in userGroup.Roles select r.RoleID
            };
        }
        public UserGroup ToUserGroup()
        {
            return new UserGroup
            {
                ParentID = this.ParentID??Guid.Empty,
                UserGroupName = this.UserGroupName,
                Roles = from r in this.Roles select new RoleBasic{
                    RoleID = r                     
                }
            };
        }
    }
}
